﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.Services;
using jQueryDemo.BusinessObject.Entity;
using jQueryDemo.Class;
using System.IO;

namespace jQueryDemo
{
    public partial class Advance : PageBase
    {
        #region Helper
        /// <summary>
        /// Load theme
        /// </summary>
        private void loadThemeCombo()
        {
            if (!string.IsNullOrEmpty(ddlTheme.Text)) ThemeName = ddlTheme.Text;
            ddlTheme.Items.Clear();
            DirectoryInfo themeDir = new DirectoryInfo(Server.MapPath("~/CSS/Themes"));
            DirectoryInfo[] themes = themeDir.GetDirectories();
            foreach (DirectoryInfo dir in themes)
            {
                ddlTheme.Items.Add(dir.Name);
            }
            ddlTheme.Items.FindByText(ThemeName).Selected = true;    
        }
        #endregion

        #region Web methods
        /// <summary>
        /// Search client
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [WebMethod]
        public static string SearchClientByName(string name)
        {
            string result = string.Empty;
            List<Client> clients = (new Client()).SearchClientByName(name);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = serializer.Serialize(clients);

            return result;
        }

        /// <summary>
        /// Get categories list
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetAllServiceCategories()
        {
            string result = string.Empty;
            ServiceCategory svc = new ServiceCategory();
            svc.DomainId = 1;
            ServiceCategory[] categories = svc.LoadAll();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = serializer.Serialize(categories);

            return result;
        }

        /// <summary>
        /// Get services list
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetAllServices(string categoryId)
        {
            string result = string.Empty;
            Service sev = new Service();
            sev.ServiceCategoryId = int.Parse(categoryId);

            Service[] services = sev.LoadAll();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            result = serializer.Serialize(services);

            return result;
        }
        #endregion

        #region Form events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            loadThemeCombo();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.PathAndQuery);
        }
        #endregion
    }
}